package com.stylefeng.guns.modular.vpn_server_info.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.base.tips.SuccessTip;
import com.stylefeng.guns.core.common.constant.DatasourceEnum;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.modular.vpn.config.properties.SftpProperties;
import com.stylefeng.guns.modular.vpn.utils.SftpUtil;
import com.stylefeng.guns.modular.vpn_server_info.entity.VpnServerDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.model.VpnServer;
import com.stylefeng.guns.modular.vpn_server_info.service.IVpnServerService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * VPN服务器管理控制器
 *
 * @author fengshuonan
 * @Date 2020-03-02 16:32:11
 */
@Controller
@RequestMapping("/vpnServer")
public class VpnServerController extends BaseController {

    private String PREFIX = "/vpn_server_info/vpnServer/";
    private final static Logger logger = LoggerFactory.getLogger(VpnServerController.class);

    @Autowired
    private IVpnServerService vpnServerService;

    @Value("${img-host-url}")
    private String imgHostUrl;

    /**
     * 跳转到VPN服务器管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "vpnServer.html";
    }

    /**
     * 跳转到添加VPN服务器管理
     */
    @RequestMapping("/vpnServer_add")
    public String vpnServerAdd() {
        return PREFIX + "vpnServer_add.html";
    }

    /**
     * 跳转到修改VPN服务器管理
     */
    @RequestMapping("/vpnServer_update/{vpnServerId}")
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public String vpnServerUpdate(@PathVariable Integer vpnServerId, Model model) {
        VpnServer vpnServer = vpnServerService.selectById(vpnServerId);
        vpnServer.setSourcePath(imgHostUrl+vpnServer.getSourcePath());
        model.addAttribute("item",vpnServer);
        LogObjectHolder.me().set(vpnServer);
        return PREFIX + "vpnServer_edit.html";
    }

    /**
     * 获取VPN服务器管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object list() {
        return vpnServerService.serverList();
    }

    /**
     * 新增VPN服务器管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@Valid VpnServerDTO vpnServerDTO) {
        vpnServerService.addServerPeer(vpnServerDTO);
        return SUCCESS_TIP;
    }

    /**
     * 启用、禁用VPN服务器管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer vpnServerId,
                         @RequestParam Integer status) {
        vpnServerService.updateServerPeer(vpnServerId,status);
        return SUCCESS_TIP;
    }

    /**
     * 修改VPN服务器管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object update(VpnServerDTO vpnServerDTO) {
        vpnServerService.updateServer(vpnServerDTO);
        return SUCCESS_TIP;
    }

    /**
     * VPN服务器管理详情
     */
    @RequestMapping(value = "/detail/{vpnServerId}")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object detail(@PathVariable("vpnServerId") Integer vpnServerId) {
        return vpnServerService.selectById(vpnServerId);
    }

//    @PostMapping("/upload")
//    @ResponseBody
//    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
//    public Object uploadImage(VpnServerDTO vpnServerDTO){
//        logger.info("开始上传服务器节点图标>>>>>>>>");
//        SftpUtil imageUtil = null;
//        String remoteFile = "";
//        MultipartFile file = vpnServerDTO.getFile();
//        try {
//            String uuid = UUID.randomUUID().toString().replaceAll("-","");
//            remoteFile = imageSftp.getBasePath() + uuid + SftpUtil.getExtName(file.getOriginalFilename());
//            imageUtil = new SftpUtil(imageSftp);
//
//            imageUtil.uploadFile(remoteFile,file.getInputStream());
//
//        } catch (Exception e) {
//            logger.error("服务器节点图标上传出错，请重试....");
//            e.printStackTrace();
//        }finally {
//            if (imageUtil != null){
//                imageUtil.close();
//            }
//        }
//        logger.info("服务器节点图标上传完成<<<<<<<<<<");
//        SuccessTip successTip = new SuccessTip();
//        successTip.setCode(0);
//        return successTip;
//    }
}
